package com.core.admin.action;
import java.util.Date;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.annotation.RequiredStringValidator;
import com.annotation.StringLengthFieldValidator;
import com.annotation.Validations;
import com.annotation.validator.ExecutionValidator;
import com.annotation.validator.WebValidateErrors;
import com.cms.staticpage.impl.StaticPageSevImpl;
import com.common.exception.RepeatedIndexException;
import com.common.page.Pagination;
import com.core.admin.action.service.WebMessage;
import com.core.entity.ActivityType;
import com.core.manager.ActivityTypeMng;
import com.core.manager.impl.ActivityTypeMngImpl;

@Controller
public class ActivityTypeAct {
	@Resource(type = ExecutionValidator.class)
	ExecutionValidator exec;
	private Logger log = LoggerFactory.getLogger(StaticPageSevImpl.class);
	@Resource(type = ActivityTypeMngImpl.class)
	ActivityTypeMng activityTypeMng;

	@RequestMapping("/aty_list.do")
	public String list(Pagination page, ModelMap model) {
		activityTypeMng.getPage(page);
		model.addAttribute("page", page);
		return "activity/list";
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "typeName", message = "活动类型不能为空") }, stringLengthFields = { @StringLengthFieldValidator(fieldName = "typeName", message = "活动类型在4-20个字符之间", minLength = 4, maxLength = 20) })
	@RequestMapping("/aty_doSave.do")
	public String doSave(ActivityType activity, ModelMap model) {
		exec.validate(ActivityTypeAct.class, "doEdit");
		if (WebValidateErrors.hasErrors()) {
			return WebValidateErrors.showErrorPage(model);
		}
		activity.setGmtCreated(new Date());
		try {
			activityTypeMng.save(activity);
		} catch (RepeatedIndexException e) {
			return WebMessage.create().addErrorMessage(e.getMessage())
					.showPage(model);
		}
		return WebMessage.create().addMSGStr("增加活动类型成功").showPage(model);
	}

	@RequestMapping("/aty_doDelete.do")
	public String doDelete(Integer id, ModelMap model) {
		try {
			activityTypeMng.delete(id);
		} catch (DataIntegrityViolationException e) {
			log.error("外键引用删除失败", e);

		}
		return WebMessage.create().addMSGStr("类型删除成功").showPage(model);
	}

	@RequestMapping("/aty_save.do")
	public String save() {
		return "activity/save";
	}

	@RequestMapping("/aty_edit.do")
	public String edit(Integer id, ModelMap model) {
		model.put("id", id);
		return "activity/edit";
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "typeName", message = "活动类型不能为空") }, stringLengthFields = { @StringLengthFieldValidator(fieldName = "typeName", message = "活动类型在4-20个字符之间", minLength = 4, maxLength = 20) })
	@RequestMapping("/aty_doEdit.do")
	public String doEdit(ActivityType activity, ModelMap model) {
		exec.validate(ActivityTypeAct.class, "doEdit");
		if (WebValidateErrors.hasErrors()) {
			return WebValidateErrors.showErrorPage(model);
		}
		try {
			activityTypeMng.edit(activity);
		} catch (RepeatedIndexException e) {
			log.error("外键引用,不能删除该数据 id:" + activity.getId(), e);
			return WebMessage.create().addErrorMessage("外键引用,不能删除该数据")
					.showPage(model);
		}
		return WebMessage.create().addMSGStr("活动类型修改成功").showPage(model);
	}
}
